//超时未返回给出仍在操作的提示
$(document).ajaxStart(function (event, jqXHR, settings) {
    inProgressTimer = setTimeout(function () {
        $(".in-progress, .in-error").remove();
        var $info = $('<div class="alert alert-info in-progress" role="alert"></div>');
        $info.text("仍在操作请稍后......");
        $error.css("position", "absolute").css("zIndex", "1001").css("visibility", "hidden");
        $error.prependTo("body");
        var leftMargin = $("body").width() / 2 - $error.outerWidth() / 2;
        var topMargin = $(".nav").outerHeight() + 20;
        $error.offset({ top: topMargin, left: leftMargin }).css("visibility", "visible");
    }, 1000);
});

//返回结束提示
$(document).ajaxStop(function (event, jqXHR, settings) {
    clearTimeout(inProgressTimer);
    $(".in-progress").remove();
});

//出错给出提示
$(document).ajaxError(function (event, jqXHR, settings) {
	clearTimeout(inProgressTimer);
	var message = "请求出错：";
	if (jqXHR.status == 404) {
		message += "没有该功能";
	} else {
		var errHeaderMsg = jqXHR.getResponseHeader("Error-Json");
		if (!errHeaderMsg) {
			message += "未知错误";
			return;
		}
		errJson = $.parseJSON(errHeaderMsg);
		message += errJson.code;
		console.log(errJson.code, unescape(errJson.msg));
	}
	$(".in-progress, .in-error").remove();
	var $error = $('<div class="alert alert-danger in-error" role="alert"></div>');
	$error.text(message);
	$error.css("position", "absolute").css("zIndex", "1001").css("visibility", "hidden");
	$error.prependTo("body");
	var leftMargin = $("body").width() / 2 - $error.outerWidth() / 2;
	var topMargin = $(".nav").outerHeight() + 20;
	$error.offset({ top: leftMargin, left: topMargin }).css("visibility", "visible").fadeOut(3000);
});

var messager = {
	alert: function (title, content, type, callback) {
		if (!(title && content)) {
			console.info("$messager.alert(title:string, content:string, [type:string]), [callback:function]");
			console.info("type: [info, success, warning, danger]");
			return;
		}
		var $modal = this.init();
		$(".modal-title", $modal).text(title);
		var alert_type = null;
		if(type && typeof(type) == "string")
			alert_type = "alert-" + type;
		else
			alert_type = "alert-info";
		$(".alert", $modal).addClass(alert_type).css("margin-bottom", "10px").append('<span style="margin-right: 5px; font-size: 50px; vertical-align: middle;" class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>').append(content);
		$(".modal-footer", $modal).append('<button type="button" class="btn btn-primary" onclick="$(\'#msgbox\').modal(\'hide\');">确定</button>');
		
		if(type && typeof(type) == "function")
			callback = type;
		if(callback && typeof(callback) == "function")
			$(".btn-primary", $modal).click(function () { callback() });
		$modal.appendTo("body");
		$modal.modal('show');
	},
	confirm: function (title, content, callback) {
		if (!(title && content && callback) || typeof (callback) != "function") {
			console.info("$messager.confirm(title:string, content:string, callback:function(bool))");
			return;
		}
		var $modal = this.init();
		$(".modal-title", $modal).text(title);
		$(".alert", $modal).addClass("alert-info").css("margin-bottom", "10px").append('<span style="margin-right: 5px; font-size: 50px; vertical-align: middle;" class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>').append(content);
		$(".modal-footer", $modal).append('<button type="button" class="btn btn-default" onclick="$(\'#msgbox\').modal(\'hide\');">否</button>');
		$(".modal-footer", $modal).append('<button type="button" class="btn btn-primary" onclick="$(\'#msgbox\').modal(\'hide\');">是</button>');
		$(".btn-primary", $modal).click(function () { callback(true) });
		$(".btn-default", $modal).click(function () { callback(false) });
		$modal.appendTo("body");
		$modal.modal('show');
	},
	prompt: function (title, content, callback) {
		if (!(title && content && callback) || typeof (callback) != "function") {
			console.info("$messager.confirm(title:string, content:string, callback:function(data:string))");
			return;
		}
		var $modal = this.init();
		$(".modal-title", $modal).text(title);
		$(".alert", $modal).css("margin-bottom", "10px").append(content);
		$(".alert", $modal).append('<br/><input id="msgData" type="text" style="width: 100%;margin-top:5px;"></input>')
		$(".modal-footer", $modal).append('<button type="button" class="btn btn-primary" onclick="$(\'#msgbox\').modal(\'hide\');">确定</button>');
		$(".btn-primary", $modal).click(function (e) { callback($("#msgData", $modal).val()) });
		$modal.appendTo("body");
		$modal.modal('show');
	},
	slide: function () {

	},
	init: function () {
		$("#msgbox").modal("hide");
		var $modal = $("<div id='msgbox'>");
		$modal.addClass("modal fade").attr("tabindex", "-1").attr("role", "dialog").attr("aria-labelledby", "msgModalLabel").attr("aria-hidden", "true");
		$modal.append("<div class='modal-dialog'><div class='modal-content'></div></div>");
		$(".modal-content", $modal).append("<div class='modal-header'>");
		$(".modal-content", $modal).append("<div class='modal-body'>");
		$(".modal-content", $modal).append("<div class='modal-footer'>");
		$(".modal-header", $modal).append('<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button><h4 class="modal-title" id="msgModalLabel"></h4>');
		$(".modal-body", $modal).append('<div class="alert" role="alert" style="font-size: 18px;"></div>');
		$modal.on('hidden.bs.modal', function (e) {
			$(this).remove();
		});
		$modal.modal({ backdrop: false })
		return $modal;
	}
};

//删除首尾空格
String.prototype.trim = function () {
	return this.replace(/(^\s*)|(\s*$)/g, "");
};
/**
 * .data-grid-chkbox	check box class
 */
(function ($) {
	var opts = {
		singleSelect: false
	};
	$.fn.tinygrid = function (options) {
		$.extend(opts, options);
		this.each(function (i) {
			if (this.tagName !== "TABLE") {
				console.error("this plugin is only comfortable with TABLE element.");
				return;
			}
			bindEvent.apply(this);
		});
	};

	function bindEvent() {
		$(this).delegate(".data-grid-chkbox", "click", { table: this }, function (e) {
			e.stopPropagation();
			if (this.checked && opts.singleSelect)
				$(".data-grid-chkbox", e.data.table).not(this).each(function () {
					this.checked = false;
				});
		});
		// 点击表格行影响checkbox
		$(this).delegate("tr", "click", { table: this }, function (e) {
			var chkbox = $(".data-grid-chkbox", this)[0];
			chkbox.checked = !chkbox.checked;
			if (chkbox.checked && opts.singleSelect)
				$(".data-grid-chkbox", e.data.table).not(chkbox).each(function () {
					this.checked = false;
				});
		});
	}
})(jQuery);